home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / dev / debug / Wipeout.lha / source / tools.c < prev    next >
C/C++ Source or Header  |  1998-04-13  |  5KB  |  308 lines

  1. /*
  2.  * $Id: tools.c 1.9 1998/04/13 12:08:11 olsen Exp olsen $
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Wipeout -- Traces and munges memory and detects memory trashing
  7.  *
  8.  * Written by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  * Public Domain
  10.  */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "global.h"
  14. #endif    /* _GLOBAL_H */
  15.  
  16. /******************************************************************************/
  17.  
  18. VOID
  19. StrcpyN(LONG MaxLen,STRPTR To,const STRPTR From)
  20. {
  21.     ASSERT(To != NULL && From != NULL);
  22.  
  23.     /* copy a string, but only up to MaxLen characters */
  24.  
  25.     if(MaxLen > 0)
  26.     {
  27.         LONG Len = strlen(From);
  28.  
  29.         if(Len >= MaxLen)
  30.             Len = MaxLen - 1;
  31.  
  32.         strncpy(To,From,Len);
  33.         To[Len] = '\0';
  34.     }
  35. }
  36.  
  37. /******************************************************************************/
  38.  
  39. struct FormatContext
  40. {
  41.     STRPTR    Index;
  42.     LONG    Size;
  43.     BOOL    Overflow;
  44. };
  45.  
  46. STATIC VOID ASM
  47. StuffChar(
  48.     REG(a3)    struct FormatContext *    Context,
  49.     REG(d0) UBYTE                    Char)
  50. {
  51.     /* Is there still room? */
  52.     if(Context->Size > 0)
  53.     {
  54.         (*Context->Index) = Char;
  55.  
  56.         Context->Index++;
  57.         Context->Size--;
  58.  
  59.         /* Is there only a single character left? */
  60.         if(Context->Size == 1)
  61.         {
  62.             /* Provide null-termination. */
  63.             (*Context->Index) = '\0';
  64.  
  65.             /* Don't store any further characters. */
  66.             Context->Size = 0;
  67.         }
  68.     }
  69.     else
  70.     {
  71.         Context->Overflow = TRUE;
  72.     }
  73. }
  74.  
  75. BOOL
  76. VSPrintfN(
  77.     LONG            MaxLen,
  78.     STRPTR            Buffer,
  79.     const STRPTR    FormatString,
  80.     const va_list    VarArgs)
  81. {
  82.     BOOL result = FAILURE;
  83.  
  84.     /* format a text, but place only up to MaxLen
  85.      * characters in the output buffer (including
  86.      * the terminating NUL)
  87.      */
  88.  
  89.     ASSERT(Buffer != NULL && FormatString != NULL);
  90.  
  91.     if(MaxLen > 1)
  92.     {
  93.         struct FormatContext Context;
  94.  
  95.         Context.Index        = Buffer;
  96.         Context.Size        = MaxLen;
  97.         Context.Overflow    = FALSE;
  98.  
  99.         RawDoFmt(FormatString,(APTR)VarArgs,(VOID (*)())StuffChar,(APTR)&Context);
  100.  
  101.         if(NO Context.Overflow)
  102.             result = SUCCESS;
  103.     }
  104.  
  105.     return(result);
  106. }
  107.  
  108. BOOL
  109. SPrintfN(
  110.     LONG            MaxLen,
  111.     STRPTR            Buffer,
  112.     const STRPTR    FormatString,
  113.                     ...)
  114. {
  115.     va_list VarArgs;
  116.     BOOL result;
  117.  
  118.     /* format a text, varargs version */
  119.  
  120.     ASSERT(Buffer != NULL && FormatString != NULL);
  121.  
  122.     va_start(VarArgs,FormatString);
  123.     result = VSPrintfN(MaxLen,Buffer,FormatString,VarArgs);
  124.     va_end(VarArgs);
  125.  
  126.     return(result);
  127. }
  128.  
  129. /******************************************************************************/
  130.  
  131. BOOL
  132. DecodeNumber(
  133.     const STRPTR    number,
  134.     LONG *            valuePtr)
  135. {
  136.     BOOL decoded = FALSE;
  137.     LONG value = 0;
  138.  
  139.     /* is this a hexadecimal number? */
  140.     if(Strnicmp(number,"0x",2) == SAME || number[0] == '$')
  141.     {
  142.         STRPTR string;
  143.         UBYTE c;
  144.  
  145.         /* skip the format identifier */
  146.         if(number[0] == '$')
  147.             string = (STRPTR)number + 1;
  148.         else
  149.             string = (STRPTR)number + 2;
  150.  
  151.         decoded = TRUE;
  152.  
  153.         /* decode the number character by character */
  154.         while((c = ToLower(*string++)) != '\0')
  155.         {
  156.             if('0' <= c && c <= '9')
  157.             {
  158.                 value = (value * 16) + (c - '0');
  159.             }
  160.             else if ('a' <= c && c <= 'f')
  161.             {
  162.                 value = (value * 16) + 10 + (c - 'a');
  163.             }
  164.             else
  165.             {
  166.                 /* not in the range 0..9/a..f */
  167.                 decoded = FALSE;
  168.                 break;
  169.             }
  170.         }
  171.     }
  172.     else
  173.     {
  174.         /* decode the decimal number */
  175.         if(StrToLong((STRPTR)number,&value))
  176.         {
  177.             decoded = TRUE;
  178.         }
  179.     }
  180.  
  181.     if(decoded)
  182.     {
  183.         (*valuePtr) = value;
  184.     }
  185.  
  186.     return(decoded);
  187. }
  188.  
  189. /******************************************************************************/
  190.  
  191. STATIC VOID
  192. TimeValToDateStamp(
  193.     const struct timeval *    tv,
  194.     struct DateStamp *        ds)
  195. {
  196.     /* convert a timeval to a DateStamp */
  197.  
  198.     ds->ds_Days        = tv->tv_secs / (24 * 60 * 60);
  199.     ds->ds_Minute    = (tv->tv_secs % (24 * 60 * 60)) / 60;
  200.     ds->ds_Tick        = (tv->tv_secs % 60) * TICKS_PER_SECOND + (tv->tv_micro * TICKS_PER_SECOND) / 1000000;
  201. }
  202.  
  203. /******************************************************************************/
  204.  
  205. VOID
  206. ConvertTimeAndDate(
  207.     const struct timeval *        tv,
  208.     STRPTR                        date,
  209.     STRPTR                        time)
  210. {
  211.     struct DateTime dat;
  212.  
  213.     /* convert a timeval into a human-readable date and time text */
  214.  
  215.     ASSERT(tv != NULL);
  216.  
  217.     memset(&dat,0,sizeof(dat));
  218.  
  219.     TimeValToDateStamp(tv,&dat.dat_Stamp);
  220.  
  221.     dat.dat_Format    = FORMAT_DOS;
  222.     dat.dat_StrDate    = date;
  223.     dat.dat_StrTime    = time;
  224.  
  225.     DateToStr(&dat);
  226. }
  227.  
  228. /******************************************************************************/
  229.  
  230. struct Node *
  231. FindIName(const struct List * list,const STRPTR name)
  232. {
  233.     struct Node * result = NULL;
  234.     struct Node * node;
  235.  
  236.     /* find a name in a list, ignoring case */
  237.  
  238.     for(node = (struct Node *)list->lh_Head ;
  239.         node->ln_Succ != NULL ;
  240.         node = node->ln_Succ)
  241.     {
  242.         if(Stricmp(node->ln_Name,name) == SAME)
  243.         {
  244.             result = node;
  245.             break;
  246.         }
  247.     }
  248.  
  249.     return(result);
  250. }
  251.  
  252. /******************************************************************************/
  253.  
  254. BOOL
  255. IsTaskStillAround(const struct Task * whichTask)
  256. {
  257.     struct Node * node;
  258.     BOOL found;
  259.  
  260.     /* check whether the given task is still active and
  261.      * has not yet exited
  262.      */
  263.  
  264.     Forbid();
  265.  
  266.     found = FALSE;
  267.  
  268.     /* Looking for myself? */
  269.     if(whichTask == FindTask(NULL))
  270.     {
  271.         found = TRUE;
  272.     }
  273.  
  274.     /* Check the list of running tasks. */
  275.     if(NOT found)
  276.     {
  277.         for(node = SysBase->TaskReady.lh_Head ;
  278.             node->ln_Succ != NULL ;
  279.             node = node->ln_Succ)
  280.         {
  281.             if(node == (struct Node *)whichTask)
  282.             {
  283.                 found = TRUE;
  284.                 break;
  285.             }
  286.         }
  287.     }
  288.  
  289.     /* Check the list of waiting tasks. */
  290.     if(NOT found)
  291.     {
  292.         for(node = SysBase->TaskWait.lh_Head ;
  293.             node->ln_Succ != NULL ;
  294.             node = node->ln_Succ)
  295.         {
  296.             if(node == (struct Node *)whichTask)
  297.             {
  298.                 found = TRUE;
  299.                 break;
  300.             }
  301.         }
  302.     }
  303.  
  304.     Permit();
  305.  
  306.     return(found);
  307. }
  308.